var uploadImage = require('../../utils/uploadImg/uploadImg');
const {
  getYearMateTime
} = require('../../utils/time')
Component({

  properties: {
    value: {
      type: String,
      observer: function (newVal, oldVal, changePath) {

        var split = []
        if (newVal.indexOf(',') > 0) {
          split = newVal.split(',')
        } else if (newVal) {
          split.push(newVal)
        }


        let temp = []
        split.forEach(url => {
          temp.push({
            url: url
          })
        })
        this.setData({
          files: temp
        })
      }
    },
    edit: {
      type: Boolean,
      value: true // false
    },
    title: {
      type: String,
      value: '图片上传'
    }

  },

  lifetimes: {
    attached() {
      this.setData({
        selectFile: this.selectFile.bind(this),
        uplaodFile: this.uplaodFile.bind(this)
      })



    },
    ready: function () {
      // const query = wx.createSelectorQuery();
      // this.setData({
      //   canvas:query
      // })
   
    }
  },


  data: {
    files: [],
    windowWidth: wx.getSystemInfoSync().windowWidth,
    canvasWidthValue: 300,
    canvasHeightValue: 300,
    canvas: null,
    width: null,
    height: null,
    context: null
  },

  observers: {
    'files'(files) {
      console.log(files);
      this.change(files)
    }
  },

  methods: {

 

    change(files) {
      var temp = ''
      files.map((x, y) => {
        temp += y == files.length - 1 ? x.url : (x.url + ',')
      })
      this.triggerEvent('change', temp)

    },

    previewImage: function (e) {
      wx.previewImage({
        current: e.currentTarget.id,
        urls: this.data.files
      })
    },
    selectFile(files) {

    },
    uplaodFile(files) {


      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('some error')
        }, 1000)
      })
    },
    uploadError(e) {

    },
    uploadSuccess(e) {

    },

    delete(e) {
      const {
        index
      } = e.detail
      let files = this.data.files
      files.splice(index, 1)
      this.setData({
        files: files
      })
    },

    select(e) {
      
      let res = e.detail;
      let that = this
      let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
      let time = new Date().getTime()
      const currentTime = getYearMateTime(time);
      const currentLocation = prolist.userName;

      for (var i = 0; i < res.tempFilePaths.length; i++) {

        wx.showLoading({
          title: '上传中' + (i + 1) + '/' + res.tempFilePaths.length,
          mask: true
        })
       
      
        
        uploadImage(res.tempFilePaths[i], 'upload/',
        function (result) {

          wx.hideLoading()
          var {
            files
          } = that.data
          files.push({
            url: result
          })
          that.setData({
            files: files
          })



        },
        function (result) {

          wx.hideLoading()
          wx.showToast({
            title: `上传失败,原因${result}`,
            icon: 'none',
            duration: 1500,
            mask: true
          })

        }
      )



      }

    },






  


  }
})